package link.ahsj.sqlserver;

import link.ahsj.sqlserver.exception.BaseBusinessException;
import link.ahsj.sqlserver.service.BusiessService;
import link.ahsj.sqlserver.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

//@PropertySource(value = "classpath:/application.properties")
@Component
@Slf4j
public class TestTask {
    @Autowired
    private BusiessService busiessService;


    @Scheduled(cron = "0 0 10 * * ?")
    //@Scheduled(fixedDelay = 100000)
    public void sendMsg1() {
        //每天的第一次任务,将前一天的错误定时任务记录删除
        File file = new File("D:/易联众药企通/yddjjk/taskError.properties");
        if (file.exists()) {
            file.delete();
        }
        try {
            String startTime = "";
            String endTime = "";

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0));
            LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.of(10,0,0));
            startTime=today_start.format(dateTimeFormatter);
            endTime=today_end.format(dateTimeFormatter);

            list(startTime, endTime);
        } catch (BaseBusinessException e) {

            //此处生成日志文件,默认产生此异常以后当前数据都没有发送成功
            FileUtil.taskErrorRecord(1);
            return;
        } catch (Exception e) {

        }
    }



    @Scheduled(cron = "0 0 16 * * ?")
    public void sendMsg2() {

        try {
            String startTime = "";
            String endTime = "";

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.of(10, 0, 0));
            LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.of(16, 0, 0));
            startTime = today_start.format(dateTimeFormatter);
            endTime = today_end.format(dateTimeFormatter);

            list(startTime, endTime);
        } catch (BaseBusinessException e) {

            //此处生成日志文件,默认产生此异常以后当前数据都没有发送成功
            FileUtil.taskErrorRecord(2);
            return;
        } catch (Exception e) {
            //do nothing
        }
        sendErrorTask();
    }

    @Scheduled(cron = "0 0 20 * * ?")
    public void sendMsg3() {

        try {
            String startTime = "";
            String endTime = "";

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.of(16,0,0));
            LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.of(20,0,0));
            startTime=today_start.format(dateTimeFormatter);
            endTime=today_end.format(dateTimeFormatter);

            list(startTime, endTime);
        } catch (BaseBusinessException e) {
            //这里最后一次,不记录日志文件了
            //此处生成日志文件,默认产生此异常以后当前数据都没有发送成功
            //FileUtil.taskErrorRecord(3);
            return;
        } catch (Exception e) {
            //do nothing
        }
        sendErrorTask();

    }

    /**
     * 将之前未完成的任务重新发送
     */
    private void sendErrorTask() {
        List<String> list = FileUtil.getTaskError();
        if (!CollectionUtils.isEmpty(list)) {
            for (String s : list) {
                int i = Integer.valueOf(s);
                String startTime="";
                String endTime="";

                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //System.out.println(today_start.format(dateTimeFormatter));
                if (1==i){
                    LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0));
                    LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.of(10,0,0));
                    startTime=today_start.format(dateTimeFormatter);
                    endTime=today_end.format(dateTimeFormatter);
                }else if(2==i){
                    LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.of(10,0,0));
                    LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.of(16,0,0));
                    startTime=today_start.format(dateTimeFormatter);
                    endTime=today_end.format(dateTimeFormatter);
                }
                try {
                    list(startTime, endTime);
                } catch (Exception e) {
                    return;
                }
            }
        }
        //!!!!  每次执行完成后,都需要删除此文件,否则下次还会执行,会产生重复数据
        File file = new File("D:/易联众药企通/yddjjk/taskError.properties");
        if (file.exists()) {
            file.delete();
        }
    }


    /**
     * 开始调用各个接口`
     * @param startTime
     * @param endTime
     * @throws Exception
     */
    private void list(String startTime, String endTime) throws Exception {
        busiessService.list1(startTime, endTime);
        busiessService.list2(startTime, endTime);
        busiessService.list3(startTime, endTime);
        busiessService.list4(startTime, endTime);
        busiessService.list5(startTime, endTime);
        busiessService.list6(startTime, endTime);
        busiessService.list7(startTime, endTime);
        busiessService.list8(startTime, endTime);
        busiessService.list91(startTime, endTime);
        busiessService.list92(startTime, endTime);
        busiessService.list93(startTime, endTime);
        busiessService.list10(startTime, endTime);
    }

}